#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, f[301][301], a[301], c[301];
bool b[301]; 
int main() {
	scanf("%lld", &n);
	for (ll i = 1; i <= n; ++i)
		for (ll j = 1; j <= n; ++j)
			scanf("%lld", &f[i][j]);
	for (ll i = 1; i <= n; ++i)
		scanf("%lld", &a[i]);
	for (ll l = n; l; l--) {
		ll k = a[l];
		for (ll i = 1; i <= n; ++i)
			for (ll j = 1; j <= n; ++j)
				f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
		b[k] = true;
		ll ans = 0;
		for (ll i = 1; i <= n; ++i)
			for (ll j = 1; j <= n; ++j)
				if (b[i] && b[j]) 
					ans += f[i][j];
		c[l] = ans; 	
	}
	for (ll i = 1; i <= n; ++i)
		printf("%lld ", c[i]);
	printf("\n");
	return 0;
}
